<template>
	<!-- 页面容器 -->
	<view class="flow-container">
		<!-- 顶部Tab栏 -->
		<view class="tab-bar">
			<view v-for="(tab, index) in tabs" :key="index" class="tab-item" :class="{ active: currentTab === index }"
				@click="currentTab = index">
				{{ tab.name }}
			</view>
		</view>

		<!-- 加载状态 -->
		<view v-if="loading" class="loading-mask">
			<view class="loading-content">
				<text>加载中...</text>
			</view>
		</view>

		<!-- 流水列表区域 -->
		<view class="flow-list" v-else>
			<!-- 遍历过滤后的流水数据 -->
			<view v-for="(item, idx) in filteredFlowList" :key="idx" class="flow-item">
				<!-- 左侧：流水类型文本 -->
				<view class="flow-left">
					<text class="flow-type">{{ item.flowTypeText }}</text>
					<text class="flow-time">{{ formatDate(item.flowTime) }}</text>
				</view>
				<!-- 右侧：金额、余额 -->
				<view class="flow-right">
					<text class="flow-amount" :class="{ income: item.amount >= 0, expense: item.amount < 0 }">
						{{ item.amount >= 0 ? '+' : '' }}{{ item.amount.toFixed(2) }}
					</text>
					<text class="flow-balance">余额: {{ item.afterBalance }}</text>
				</view>
			</view>

			<!-- 无数据提示 -->
			<view class="empty-tip" v-if="filteredFlowList.length === 0">
				暂无对应类型的流水记录
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onMounted,
		onUnmounted,
		nextTick,
		watch
	} from 'vue';

	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		getZiJinLiuShui
	} from "@/api/xiaoquan.js";

	// 状态管理
	const flowList = ref([]);
	const tabs = ref([{
		name: '全部收支',
		type: ''
	}, {
		name: '充值',
		type: 1
	}, {
		name: '佣金',
		type: 2
	}, {
		name: '支出',
		type: 3
	}, {
		name: '提现',
		type: 4
	}]);
	const currentTab = ref(0);
	const loading = ref(true);
	const errorMsg = ref('');

	// 格式化流水类型文本
	const formatFlowType = (type) => {
		const typeMap = {
			1: '充值',
			2: '佣金',
			3: '支出',
			4: '提现',
			5: '其他'
		};
		return typeMap[type] || '未知类型';
	};

	// 格式化日期（使用原生JS）
	const formatDate = (dateStr) => {
		if (!dateStr) return '';
		const date = new Date(dateStr);
		return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
	};

	// 处理后的流水数据（带格式化文本）
	const handledFlowList = computed(() => {
		return flowList.value.map(item => ({
			...item,
			flowTypeText: formatFlowType(item.flowType)
		}));
	});

	// 根据当前Tab过滤流水数据
	const filteredFlowList = computed(() => {
		const currentTabType = tabs.value[currentTab.value].type;
		if (!currentTabType) {
			// 全部收支，返回所有
			return handledFlowList.value;
		}
		// 按类型过滤
		return handledFlowList.value.filter(item => item.flowType === currentTabType);
	});

	// 获取流水数据
	const fetchFlowData = async () => {
		loading.value = true;
		try {
			const res = await getZiJinLiuShui(); // 直接调用提供的方法，无需参数（可根据实际接口调整）
			if (res.code === 200 && res.data) {
				// 假设接口返回格式：{ code: 200, data: [...流水数据] }
				flowList.value = res.data || [];

				// 处理金额数据类型（如果接口返回字符串）
				flowList.value.forEach(item => {
					if (typeof item.amount === 'string') {
						item.amount = parseFloat(item.amount);
					}
				});
			} else {
				errorMsg.value = res.message || '获取流水数据失败';
				console.error(errorMsg.value);
			}
		} catch (error) {
			errorMsg.value = '网络请求失败，请稍后重试';
			console.error('获取流水数据异常:', error);
		} finally {
			loading.value = false;
		}
	};

	// 页面加载时获取数据
	onMounted(() => {
		fetchFlowData();
	});

	// 页面每次显示时刷新数据
	onShow(() => {
		fetchFlowData();
	});
</script>

<style scoped>
	/* 页面整体容器 */
	.flow-container {
		padding: 10px;
		background-color: #fff;
		min-height: 100vh;
	}

	/* Tab栏样式 */
	.tab-bar {
		display: flex;
		border-bottom: 1px solid #f5f5f5;
		position: sticky;
		top: 0;
		background-color: #fff;
		z-index: 10;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		padding: 10px 0;
		font-size: 16px;
		color: #999;
	}

	.tab-item.active {
		color: #007aff;
		border-bottom: 2px solid #007aff;
	}

	/* 加载状态 */
	.loading-mask {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(255, 255, 255, 0.7);
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 100;
	}

	.loading-content {
		background-color: #fff;
		padding: 15px 30px;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
	}

	/* 流水列表 */
	.flow-list {
		margin-top: 10px;
	}

	.flow-item {
		display: flex;
		justify-content: space-between;
		padding: 15px 0;
		border-bottom: 1px solid #f5f5f5;
	}

	.flow-left {
		font-size: 16px;
		color: #333;
	}

	.flow-time {
		font-size: 14px;
		color: #999;
		margin-top: 3px;
	}

	.flow-right {
		text-align: right;
	}

	.flow-amount {
		font-size: 16px;
		margin: 5px 0;
	}

	/* 收入、支出金额颜色区分 */
	.flow-amount.income {
		color: #00cc00;
	}

	.flow-amount.expense {
		color: #ff3333;
	}

	.flow-balance {
		font-size: 12px;
		color: #999;
	}

	/* 无数据提示 */
	.empty-tip {
		text-align: center;
		padding: 40px 0;
		color: #999;
		font-size: 14px;
	}
</style>